package com.asyf.provider.controller;

import com.asyf.provider.service.JdbcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "jdbc测试")
@RestController
@RequestMapping(value = "/jdbcTest")
@Slf4j
public class JdbcController {

    @Value("${server.port}")
    private Integer port;

    @Value("test")
    private String test;

    @Autowired
    private JdbcService jdbcService;

    @ApiOperation(value = "获取端口")
    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    public String test() {
        System.out.println(test.length());
        return "abc" + " - port:" + port;
    }

    @ApiOperation(value = "sql查询测试")
    @RequestMapping(value = "/sqlTest", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数字除数", name = "a", example = "1")
    })
    public Object sqlTest(int a) {
        Object o = jdbcService.sqlTest(a);
        return o;
    }

    @ApiOperation(value = "seata单机事务测试")
    @RequestMapping(value = "/transactionTest", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数字除数", name = "a", example = "1")
    })
    public Object transactionTest(int a) {
        Object o = jdbcService.transactionTest(a);
        return o;
    }

    @ApiOperation(value = "seata动态数据源测试")
    @RequestMapping(value = "/dynamicDataSourceTest", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数字除数", name = "a", example = "1")
    })
    public Object dynamicDataSourceTest(int a) {
        Object o = jdbcService.dynamicDataSourceTest(a);
        return o;
    }


    @ApiOperation(value = "seata动态数据源测试-事务")
    @RequestMapping(value = "/dynamicDataSourceTxTest", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数字除数", name = "a", example = "1")
    })
    public Object dynamicDataSourceTxTest(int a) {
        Object o = jdbcService.dynamicDataSourceTxTest(a);
        return o;
    }

    /**
     * 异常：io.seata.common.exception.NotSupportYetException: TEST needs to contain the primary key
     * 不支持oracle复合主键，仅仅支持mysql（需使用tcc模式）
     */
    @ApiOperation(value = "seata动态数据源测试-事务(oracle联合主键)")
    @RequestMapping(value = "/dynamicDataSourceTxCompTest", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数字除数", name = "a", example = "1")
    })
    public Object dynamicDataSourceTxCompTest(int a) {
        Object o = jdbcService.dynamicDataSourceTxCompTest(a);
        return o;
    }

}
